﻿using System;

namespace ActionAndFunc
{
    class Program
    {
        delegate void action();
        delegate int func(int x, int y);
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            //定义Action委托变量，封装calculator.Report
            Action action = new Action(calculator.Report);

            //调用等价式样三联
            calculator.Report();
            action.Invoke();
            action();

            func[] funcArithmetic =
            {
                calculator.Add,
                calculator.Sub
            };

            for (int i = 0; i < funcArithmetic.Length; i++)
            {
                Console.WriteLine($"使用 operations[{i}]:");
                ProcessAndDisplayNumber(funcArithmetic[i], 2, 2);
                ProcessAndDisplayNumber(funcArithmetic[i], 7, 7);
                ProcessAndDisplayNumber(funcArithmetic[i], 10, 10);
                Console.WriteLine();
            }

            //Func<参数1类型,参数2类型,返回值类型> func1 = new Func<参数1类型,参数2类型,返回值类型>(封装方法);
            //Func<int, int, double> func1 = new Func <int, int, double>(calculator.Add);
            //Func<int, int, double > func2 = new Func<int, int, double>(calculator.Sub);
            //int x = 100;
            //int y = 200;
            //double z1,z2,z3= 0;
            //z1 = calculator.Add(x, y);
            //z2 = func1.Invoke(x, y);
            //z3 = func1(x, y);
            //Console.WriteLine($"z1={z1}  z2={z2} z3={z3}");
            //z1 = func2.Invoke(x, y);
            //Console.WriteLine(z1);

        }
        static void ProcessAndDisplayNumber(func action, int x, int y) =>
       Console.WriteLine($"原始数据：{x}和{y}, 对数据处理后： {action(x, y)}");
    }
}
